package com.yw.pms.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yw.pms.dao.entity.Operator;
import com.yw.pms.dao.entity.Problem;
import com.yw.pms.dao.entity.ProblemType;
import com.yw.pms.dao.entity.Product;
import com.yw.pms.service.ProblemService;
import com.yw.pms.service.ProblemTypeService;
import com.yw.pms.service.ProductService;
import com.yw.pms.utils.CommonResp;

/**
 * @Description: 问题操作控制器
 * @author Tenny
 * @date 2017年9月1日 下午3:06:05
 */ 
@Controller
@RequestMapping("/problem")
public class ProblemController {
	private final static Logger logger = LoggerFactory.getLogger(ProblemController.class);
	@Resource
	private ProblemService problemService;
	
	@Resource
	private ProductService productService;
	
	@Resource
	private ProblemTypeService problemTypeService;
	
	/**
	 * @Description: 跳转到新增问题页面
	 * @author Tenny
	 * @date 2017年12月29日 上午10:29:17
	 */
	@RequestMapping("/toAddProblemPage")
	String toAddProblemPage(Model model,Product product,ProblemType problemType){
		CommonResp<List<Product>> resp = productService.getAllProduct(product);
		CommonResp<List<ProblemType>> respType = problemTypeService.getAllProblemType(problemType);
		model.addAttribute("productList", resp.getData());
		model.addAttribute("problemTypeList", respType.getData());
		return "addProblem";
	}
	
	/**
	 * @Description: 新增问题信息
	 * @author Tenny
	 * @date 2017年9月1日 下午3:29:28
	 */
	@RequestMapping(value="/addProblem",method=RequestMethod.POST)
	@ResponseBody CommonResp<Boolean> addProblem(Problem problem,HttpSession session){
//		problem.setPictureUrl("/opt/pcp/");
		Operator operator = getOperator(session);
		problem.setOperatorId(operator.getId());
//		problem.setOperatorName(operator.getName());
		return problemService.addProblem(problem);
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody CommonResp<Boolean> delete(String problemId){
		return problemService.delete(problemId);
	}

	private Operator getOperator(HttpSession session) {
		Operator operator = (Operator) session.getAttribute("operator");
		return operator;
	}
	
	/**
	 * @Description: 跳转到更新问题页面
	 * @author Tenny
	 * @date 2017年9月3日 上午11:23:41
	 */
	@RequestMapping("/toUpdateProblemPage")
	String toUpdateProblemPage(String problemId,Model model){
		CommonResp<Problem> resp = problemService.getProblemById(problemId);
		if(resp.isSuccess()) {
			model.addAttribute("problem", resp.getData());
		}
		return "updateProblemPage";
	}
	
	/**
	 * @Description: 更新问题
	 * @author Tenny
	 * @date 2017年9月3日 上午12:49:36
	 */
	@RequestMapping(value="/updateProblem",method=RequestMethod.POST)
	@ResponseBody CommonResp<Boolean> updateProblem(Problem problem,HttpSession session){
		Operator operator = getOperator(session);
		problem.setOperatorId(operator.getId());
//		problem.setOperatorName(operator.getName());
		return problemService.update(problem);
	}
	
	/**
	 * @Description: 查看问题详细信息
	 * @author Tenny
	 * @date 2017年9月3日 下午3:40:43
	 */
	@RequestMapping("/queryProblem")
	@ResponseBody CommonResp<Problem> queryProblem(String problemId){
		CommonResp<Problem> commonResp = problemService.getProblemById(problemId);
		return commonResp;
	}
	
	/**
	 * @Description: 问题列表分页查询
	 * @author Tenny
	 * @date 2017年9月3日 下午3:00:01
	 */
	@RequestMapping("/problemList")
	@ResponseBody CommonResp<List<Problem>> problemList(Problem problem,HttpSession session){
		return problemService.queryProblem(problem);
	}
	
	@RequestMapping("/upload")
	public  @ResponseBody String  uploads(HttpServletRequest request, HttpServletResponse response, @RequestParam("yourFileName") MultipartFile file){
		String picUploadPath = "D:/files/workspace/PMS/src/main/webapp/static/image/test/";
		logger.info("文件上传");
		String fileName = file.getOriginalFilename();
		fileName = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + fileName.substring(fileName.indexOf('.'));
		logger.info("file name: " + fileName);
		File targetFile = new File(picUploadPath, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
			logger.error("上传文件失败", e);
		}
		String fileUrl = "/static/image/test/"+fileName;
		String result = "{\"errno\":0,\"data\":[\""+fileUrl+"\"]}";
		return result;
	}
	public static void main(String[] args) {
		outer:
        for (int i = 0; i < args.length; i++) {
			break outer;
		}
		Integer b = 3,c=3,d=128,e=128;
		int f=140;
		System.out.println(c==b);
		System.out.println(d==e);
		System.out.println(d==f);
	}
}
